home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / bor_ti.exe / TI1031.ASC < prev    next >
Text File  |  1992-10-23  |  18KB  |  727 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1031
  9.   VERSION  :  3.x
  10.        OS  :  DOS
  11.      DATE  :  October 23, 1992                        PAGE  :  1/11
  12.  
  13.     TITLE  :  Some Graphics primitives using 256-Color VGA Mode.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   //   A graphics library demonstrating the simplicity of fast
  20.   //   graphics in 320x200x256-color VGA mode.  The code is not
  21.   //   fully optimized but it is pretty close; an extra 5% or so
  22.   //   might be squeezed out of the putimage() for example.  Be
  23.   //   careful when altering pseuodoregisters; while doing one
  24.   //   load the compiler may trash another register which was just
  25.   //   loaded with something pertinent.
  26.  
  27.  
  28.   //   Functions provided include point, row, and array (image)
  29.   //   drawing functions, as well as palette functions using BIOS
  30.   //   and line-drawing and circle-drawing functions.
  31.  
  32.   //   Main() demonstrates most of the functions in a pretty and
  33.   //   interesting way.
  34.  
  35.  
  36.   void GrInit(void);
  37.   void GrClose(void);
  38.   unsigned char GetPixel(unsigned int x, unsigned int y);
  39.   void PutPixel(unsigned int x, unsigned int y, unsigned char
  40.   color);
  41.   void FillRow(unsigned x, unsigned y, unsigned char color,
  42.   unsigned length);
  43.   void PutRow(unsigned x, unsigned y, char *data, unsigned length);
  44.   void GetRow(unsigned x, unsigned y, char *data, unsigned length);
  45.   void PutImage(unsigned x, unsigned y, char *data, unsigned rows,
  46.   unsigned cols);
  47.   void GetImage(unsigned x, unsigned y, char *data, unsigned rows,
  48.   unsigned cols);
  49.   void GetAllPalette(char (*palette)[3]);
  50.   void SetAllPalette( char (*palette)[3]);
  51.   void Circle(int x, int y, int radius, unsigned char color);
  52.   void Line(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
  53.             unsigned char color);
  54.  
  55.  
  56.  
  57.   #define VIDEOINT 0x10
  58.  
  59.   #pragma inline
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1031
  75.   VERSION  :  3.x
  76.        OS  :  DOS
  77.      DATE  :  October 23, 1992                        PAGE  :  2/11
  78.  
  79.     TITLE  :  Some Graphics primitives using 256-Color VGA Mode.
  80.  
  81.  
  82.  
  83.  
  84.   // 'dos.h' is essential
  85.   #include <dos.h>
  86.   // we need 'math.h' for sqrt() in Circle() and abs() in Line()
  87.   #include <math.h>
  88.   // we need 'conio.h' for getch()
  89.   #include <conio.h>
  90.  
  91.   int rowOffset[200];  // as an optimization,
  92.                        // rowOffset will be used as a table to
  93.                        // avoid the multiplication column+(320*row)
  94.                        // when getting the actual memory address of
  95.                        // a pixel or a row
  96.  
  97.  
  98.  
  99.   //   GrInit() initializes the rowOffset table and switches video
  100.   //   modes to 320x200 with 256 colors
  101.   void GrInit()
  102.   {
  103.        int i;
  104.        // Pre-setup our multipications for column,
  105.        //row -> screen address
  106.        for (i=0; i<200; ++i)
  107.        {
  108.             rowOffset[i]=i*320;
  109.        }
  110.        // Set video mode to 320x200 --
  111.        // sophisticated users may wish to research checking
  112.        // for the availability of VGA/MCGA prior to switching
  113.        // modes.
  114.  
  115.        _AH=0;
  116.        _AL=0x13;
  117.        geninterrupt(VIDEOINT);
  118.   }
  119.  
  120.  
  121.  
  122.   // GrClose sets the video back into text mode
  123.   void GrClose()
  124.   {
  125.        //   set video mode 80x25 color text -- we're assuming
  126.        //   that that's available
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1031
  141.   VERSION  :  3.x
  142.        OS  :  DOS
  143.      DATE  :  October 23, 1992                        PAGE  :  3/11
  144.  
  145.     TITLE  :  Some Graphics primitives using 256-Color VGA Mode.
  146.  
  147.  
  148.  
  149.  
  150.        //   if we're using VGA
  151.        _AH=0;
  152.        _AL=0x3;
  153.        geninterrupt(VIDEOINT);
  154.   }
  155.  
  156.  
  157.  
  158.   //   GetPixel returns the pixel value at a given (x,y) screen
  159.   //   location
  160.   unsigned char GetPixel(unsigned int x, unsigned int y)
  161.   {
  162.        return *((char far *) 0xA0000000L+ (x+rowOffset[y]));
  163.   }
  164.  
  165.  
  166.  
  167.   //   PutPixel changes the pixel at a given (x,y) screen location
  168.   //   to value 'color'
  169.   void PutPixel( unsigned int x, unsigned int y,
  170.                  unsigned char color)
  171.   {
  172.        *((char far *) 0xA0000000L + (x+rowOffset[y]))=color;
  173.   }
  174.  
  175.  
  176.  
  177.   //   FillRow writes a row of 'color' color pixels of length
  178.   //   'length' starting at screen location (x,y).
  179.   void FillRow(  unsigned x, unsigned y, unsigned char color,
  180.                  unsigned length )
  181.   {
  182.        _ES=0xA000;           // The segment for screen memory
  183.        _DI=x+rowOffset[y];   // The offset in screen to write at
  184.        _CX=length;           // Number of bytes for rep stosb
  185.        _AL=color;            // The value stosb will store in video
  186.                              // memory
  187.        asm {
  188.                  cld         // set direction flag so DI
  189.                              // will increment
  190.                  rep stosb   // zap a row of color AL out to
  191.                              // video memory al->[es:di]
  192.            }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1031
  207.   VERSION  :  3.x
  208.        OS  :  DOS
  209.      DATE  :  October 23, 1992                        PAGE  :  4/11
  210.  
  211.     TITLE  :  Some Graphics primitives using 256-Color VGA Mode.
  212.  
  213.  
  214.  
  215.  
  216.   }
  217.  
  218.  
  219.  
  220.   //   PutRow transfers a row of pixels from 'data' to screen
  221.   //   starting at (x,y) and continuing for 'length'.  A rep
  222.   //   movsw instead of rep movsb might optimize this slightly for
  223.   //   even-length rows
  224.   void PutRow(unsigned x, unsigned y, char *data, unsigned length)
  225.   {
  226.   //   You may remove all lines marked with '***' if 'data' is a
  227.   //   near pointer (i.e. if 'data' was not allocated from the far
  228.   //   heap and was not declared as 'far' data.)  Remove them all
  229.   //   if you remove any.
  230.  
  231.        _ES=0xA000;
  232.        _DI=x+rowOffset[y];      //   the offset in video memory for
  233.                                 //   the row start
  234.        _SI=FP_OFF(data);
  235.                                 // load AX last since many C
  236.                                 // instructions will trash AX
  237.        _AX=FP_SEG(data);        // store data's segment in AX for
  238.                                 // now         ***
  239.        asm  {
  240.                  push ds        // Save DS since we're going to
  241.                                 // alter it    ***
  242.                  mov ds,ax      // load register from AX (loaded
  243.                                 //  above)     ***
  244.                  mov cx,length  // we're moving 'length' bytes
  245.                                 // total
  246.                  cld            // clear the direction flag to
  247.                                 // increment si and di
  248.                  rep movsb      // dump the whole row out to video
  249.                                 // memory [ds:si]->[es:di]
  250.                  pop ds         // Restore DS  ***
  251.             }
  252.   }
  253.  
  254.  
  255.  
  256.   //   GetRow() copies a row of pixels of length 'length' out of
  257.   //   video memory into the buffer at 'data'.  rep movsw might be
  258.   //   slightly faster here also.
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1031
  273.   VERSION  :  3.x
  274.        OS  :  DOS
  275.      DATE  :  October 23, 1992                        PAGE  :  5/11
  276.  
  277.     TITLE  :  Some Graphics primitives using 256-Color VGA Mode.
  278.  
  279.  
  280.  
  281.  
  282.   void GetRow(unsigned x, unsigned y, char *data, unsigned length)
  283.   {
  284.        _SI=x+rowOffset[y];      // the offset in video memory for
  285.                                 // the row start
  286.        _ES=FP_SEG(data);        // set up ES:DI to point to data
  287.        _DI=FP_OFF(data);
  288.        _AX=0xA000;              // load video segment in AX
  289.        asm  {
  290.                  push ds        // save DS since we have to use DS
  291.                                 // for copying
  292.                  mov ds,ax      // load DS with segment for video
  293.                                 // memory from AX (0xA000)
  294.                  mov cx,length  // we're moving 'length' bytes
  295.                                 // total
  296.                  cld            // clear the direction flag to
  297.                                 // increment si and di
  298.